package com.tg.service.flow;

import com.google.common.collect.Lists;
import com.tg.service.flow.ui.superFlow.*;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;

@RestController
public class FlowAdminController {

    @RequestMapping(value = "/flow/load")
    public SpFlow load(String req) {

        SpFlow spFlow = EngineTest.OUT_DTO;
        if (spFlow == null || CollectionUtils.isEmpty(spFlow.getNodes()) || CollectionUtils.isEmpty(spFlow.getLinks())) {
            ArrayList<SpFlowNode> nodes = Lists.newArrayList();

            SpFlowNodeMeta meta1 = new SpFlowNodeMeta();
            meta1.setBeFirst(true);
            meta1.setBeLast(false);
            meta1.setId("nodeS3WgFnzCI15X58Qw");
            meta1.setKey("metaRule1");
            meta1.setNextIds(Lists.newArrayList("2"));
            meta1.setCondition(null);

            meta1.setName("测试节点1");
            meta1.setProp("start");

            SpFlowNode spFlowNode1 = new SpFlowNode();
            spFlowNode1.setId(meta1.getId());
            spFlowNode1.setWidth(200);
            spFlowNode1.setHeight(80);
            spFlowNode1.setCoordinate(Lists.newArrayList(-644, -148));
            spFlowNode1.setMeta(meta1);

            SpFlowNodeMeta meta2 = new SpFlowNodeMeta();
            meta2.setBeFirst(false);
            meta2.setBeLast(false);
            meta2.setId("nodefHsy9uJObPtdHZv1");
            meta2.setKey("metaRule2");
            meta2.setNextIds(null);
            meta2.setCondition("#tuple.req.getInteger('age')>20");
            meta2.setName("测试节点2");
            meta2.setProp("approval");

            SpFlowNode spFlowNode2 = new SpFlowNode();
            spFlowNode2.setId(meta2.getId());
            spFlowNode2.setWidth(160);
            spFlowNode2.setHeight(80);
            spFlowNode2.setCoordinate(Lists.newArrayList(-200, -148));
            spFlowNode2.setMeta(meta2);

            nodes.add(spFlowNode1);
            nodes.add(spFlowNode2);

            //link
            SpFlowLink link1 = new SpFlowLink();
            link1.setId("linkcs9ZhumWeTHrtUy8");
            link1.setStartId(spFlowNode1.getId());
            link1.setEndId(spFlowNode2.getId());
            link1.setStartAt(Lists.newArrayList(100, 40));
            link1.setEndAt(Lists.newArrayList(0, 40));
            SpFlowLinkMeta linkMeta1 = new SpFlowLinkMeta();
            linkMeta1.setDesc(spFlowNode2.getMeta().getCondition());

            link1.setMeta(linkMeta1);

            SpFlow outDTO = new SpFlow();
            outDTO.setLinks(Lists.newArrayList(link1));
            outDTO.setNodes(nodes);

            EngineTest.OUT_DTO = outDTO;

        }

        return EngineTest.OUT_DTO;
    }

    @RequestMapping(value = "/flow/save")
    public SpFlow load(@RequestBody SpFlow flow) {

        SpFlowService service = new SpFlowService();
        service.save(flow);

        return EngineTest.OUT_DTO;
    }
}
